/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.tutwiz.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.tutwiz.model.Tutorial;
import net.tutwiz.persistence.TutorialPersistenceManager;

/**
 *
 * @author danielwirdehall
 */
public class TutorialController extends Controller {

    private TutorialPersistenceManager pm = new TutorialPersistenceManager();

    public TutorialController(String[] params, HttpServletRequest request, HttpServletResponse response) {
        super(params, request, response);
    }

	// Anropar metoden för den action som ska köras
    @Override
    public void doAction() {
        if (this.action.equals("")) {
            // Default action
            this.all();
        } else if (this.action.equals("all")) {
            this.all();
        } else if (this.action.equals("show")) {
            this.show();
        }
    }

    private void all() {
        List<Tutorial> tutorials = pm.getAll();
        this.render(tutorials);
    }

    private void show() {
        if (parameters.length > 0) {
            int id;
			try {
				id = Integer.parseInt(parameters[0]);
			} catch (Exception ex) {
				id = -1;
			}
			if (id > 0) {
                            Tutorial tutorial = pm.get(Long.valueOf(id));
                            this.render(tutorial);
            } else {
                this.niceDie("Tutorial ID must be an integer!", 400);
            }
        } else {
            this.niceDie("Tutorial ID missing!", 400);
        }
    }
}
